package com.lyshop.jinxiang.adapters;

import android.util.SparseArray;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;


import com.lyshop.jinxiang.fragments.FollowMeFragment;
import com.lyshop.jinxiang.fragments.MyFollowFragment;

import java.util.ArrayList;

/**
 * Created by Mr on 2018/11/8.
 */

public class FollowViewpagerAdapter extends FragmentPagerAdapter {
    private ArrayList<Fragment> fragments;
    private SparseArray<String> tags = new SparseArray<>();
    private FragmentManager fragmentManager;

    public FollowViewpagerAdapter(FragmentManager fm) {
        super(fm);
        this.fragmentManager = fm;
        this.fragments = new ArrayList<>();
        fragments.add(new MyFollowFragment());
        fragments.add(new FollowMeFragment());
//        fragments.add(new ReceiveGiftFragment());
    }

    public Fragment getFragmentByTag(int position) {
        return fragmentManager.findFragmentByTag(tags.get(position));
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        String name = makeFragmentName(container.getId(), position);
        tags.put(position, name);
        return super.instantiateItem(container, position);
    }

    private static String makeFragmentName(int viewId, long id) {
        return "android:switcher:" + viewId + ":" + id;
    }
}
